import { _decorator, MeshRenderer, Node,Vec3,v3, tween,Component, ParticleSystemComponent } from 'cc';
import { Bullet } from '../config/BaseData';
import GDef from '../config/GDef';
import BaseView from '../game/BaseView';
import Tool from '../tool/Tool';

const { ccclass, property } = _decorator;

@ccclass('classBullet')
export class classBullet extends BaseView {
    private nodeBall: Node = null;
    private nodeTail: Node = null;
    private bullet: Bullet = null;
    private arrowMeshRenderer: MeshRenderer[] = null;
    private jsonArmType: Object = null;
    private jsonArm: Object = null;
    private distance: number = 0;
    private speed: number = 0;


    onLoad() {
    }

    start() {
    }


    /**初始化 */
    init(jsonArmType: Object,jsonArm: Object) {
        this.jsonArmType = jsonArmType;
        this.jsonArm = jsonArm;
        //拖尾
        if(!this.nodeTail)
        {
            var self = this;
            Tool.createNode(GDef.pack2,GDef.preTail1).then((nodeMesh)=>{
                self.nodeTail = nodeMesh;
            });
        }
    }
    //子弹
    setBall(node: Node) {
        var self = this;
        if(this.nodeBall)this.nodeBall.destroy();
        if(node)this.nodeBall = node;
        if(this.jsonArmType.Tid == 3)
        {
            this.tween = tween(this.nodeBall).by(0.3,{eulerAngles:v3(360,0,0)}).repeatForever();
        }
        if(this.tween)this.tween.stop();
        this.nodeBall.setRotationFromEuler(v3(this.jsonArmType.rotateArrow[0],this.jsonArmType.rotateArrow[1],this.jsonArmType.rotateArrow[2]));
        this.nodeBall.parent = this.node;
        this.nodeBall.active = false;
        if(this && this.jsonArmType && this.jsonArmType.modelPackage)
        {
            Tool.loadTexture(this.jsonArmType.modelPackage,this.jsonArmType.texturePath + "1").then((texture)=>{
                self.arrowMeshRenderer = self.nodeBall.getComponentsInChildren(MeshRenderer);
                for(let i = 0; i < self.arrowMeshRenderer.length; i++)
                {
                    self.arrowMeshRenderer[i].shadowCastingMode = 1;
                    self.arrowMeshRenderer[i].material.setProperty("mainTexture", texture);
                }
            });
        }
    }
    startFly(bullet: Bullet) {
        this.emit(GDef.wdEvent.playEffect,"ArrowRelease");
        this.bullet = bullet;
        this.speed = GDef.moveSpeedBullet * 0.1;
        this.distance = Tool.getDistance(this.bullet.targetPos,this.bullet.pos);
        this.node.scale = v3(this.bullet.jsonArmType.scaleArrow * 0.001,this.bullet.jsonArmType.scaleArrow * 0.001,this.bullet.jsonArmType.scaleArrow * 0.001);
        this.scale = this.node.scale;
        this.node.setPosition(this.bullet.pos);
        this.node.lookAt(this.bullet.targetPos,v3(0,1,0));
        if(this.nodeBall)this.nodeBall.active = true;
        if(this.nodeTail)
        {
            this.nodeTail.parent = this.node;
        }
        //斧头旋转
        console.log("this.jsonArmType:",this.jsonArmType);
        if(this.jsonArmType.Tid == 3)
        {
            if(this.tween)
            {
                this.tween.start();
            }
        }
        //从小到da
        tween(this.node).set({scale:v3(0,0,0)}).to(0.3,{scale:v3(this.scale,this.scale,this.scale)}).start();
    }

    clear(){
        //子弹消失
        // if(this.nodeTail)
        // {
        //     this.nodeTail.parent = null;
        // }
        if(this.jsonArmType.Tid == 3)
        {
            if(this.tween)this.tween.stop();
        }
        if(this.nodeBall && this.nodeBall.active)this.nodeBall.active = false;
        this.node.scale = v3(0,0,0);
        if(this.bullet)this.node.setPosition(this.bullet.pos);
        this.bullet = null;
        this.node.active = false;
    }
    onDisable(){
        this.clear();
    }

    update(dt: number){
        if(GDef.intStat != 1)
        {
            this.clear();
        }
        if(!this.bullet)return;
        let dis = Tool.getDistance(this.bullet.targetPos,this.node.position);
        let ratio = 1 - dis / this.distance;
        //已经命中
        if(dis <= GDef.moveSpeedBullet * dt)
        {
            this.emit(GDef.wdEvent.playEffect,"ArrowImpact");
            //敌人还活着
            this.emit(GDef.wdEvent.attack,this.bullet.enemyId,this.bullet.jsonArm.attack);
            this.clear();
        }
        //未到达终点
        else
        {
            this.speed = Math.min(this.speed + GDef.moveSpeedBullet * dt * 0.1,GDef.moveSpeedBullet * dt);
            if(this.nodeBall && !this.nodeBall.active)this.nodeBall.active = true;
            let pos = this.bullet.targetPos;
            let disz = pos.z - this.node.position.z;
            let disx = pos.x - this.node.position.x;
            let z = Math.abs(disz);
            let x = Math.abs(disx);
            let xx = 0;
            let zz = 0;
            if(x == 0)zz = this.speed;
            else if(z == 0)xx = this.speed;
            else
            {
                let ratio = z / x;
                xx = this.speed / (1 + ratio);
                zz = ratio * xx;
            }
            if(disx < 0)xx = -xx;
            if(disz < 0)zz = -zz;
            this.node.setPosition(v3(this.node.position.x + xx,this.bullet.pos.y + (this.bullet.targetPos.y - this.bullet.pos.y) * ratio,this.node.position.z + zz));
        }
    }
}
